package linc.spring.framework;

import linc.spring.framework.annotation.Component;
import linc.spring.framework.annotation.Value;

import java.util.stream.Stream;

/**
 * @author yqlin
 * @date 2022/3/14 8:22 下午
 * @description
 */
@Component
public class ValueAnnotationBeanPostProcessor implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) {
        Stream.of(bean.getClass().getDeclaredFields()).forEach(field -> {
            if (field.isAnnotationPresent(Value.class)) {
                try {
                    field.setAccessible(true);
                    field.set(bean, field.getAnnotation(Value.class).value());
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        });
        return bean;
    }
}
